﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing.Imaging;

public partial class CMSV2_Controls_News_NewsImage : System.Web.UI.UserControl
{
    public Guid NewsId
    {
        get
        {
            if (!String.IsNullOrEmpty(Request["NewsId"]))
            {
                return new Guid(Request["NewsId"]);
            }
            return Guid.Empty;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ResetImage();
        }
    }

    private void ResetImage()
    {
        if (!Guid.Empty.Equals(NewsId))
        {

            string filename = ResolveUrl("~/news/" + NewsId.ToString() + ".");
            if (File.Exists(MapPath(filename + "png")))
            {
                uiImgNewsImage.ImageUrl = filename + "png" + "?uid=" + Guid.NewGuid().ToString();
            }
            else if (File.Exists(MapPath(filename + "jpg")))
            {
                uiImgNewsImage.ImageUrl = filename + "jpg" + "?uid=" + Guid.NewGuid().ToString();
            }
        }
    }

    public void uiImageUploader_OnImageTransformationComplte(object sender, System.Drawing.Image transformedImage)
    {
        string filename = ResolveUrl("~/news/" + NewsId.ToString() + ".");
        if (File.Exists(MapPath(filename + "jpg")))
        {
            File.Delete(MapPath(filename + "jpg"));
        }

        transformedImage.Save(MapPath(filename + "png"), ImageFormat.Png);

        ResetImage();
    }
}
